package zhtt;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import zhtt.demo01.Blog;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by zhtt on 2017/4/30.
 */
public class BlogTest {

    @Test
    public void selectBlog_if() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();

            System.out.println("\n\n------------1.0----------------");
            Blog blog_parm=new Blog(2,"",null);
            List<Blog> blogList = sqlSession.selectList("zhtt.demo01.Blog.selectBlog", blog_parm);
            System.out.println(blogList.size()+": "+blogList);
            System.out.println("------------1.1----------------");


            System.out.println("\n\n------------2.0----------------");
            Blog blog_parm2=new Blog(2,"","blo%");
            List<Blog> blogList2 = sqlSession.selectList("zhtt.demo01.Blog.selectBlog", blog_parm2);
            System.out.println(blogList2.size()+": "+blogList2);
            System.out.println("------------2.1----------------");


        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    @Test
    public void selectBlog_choose() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();

            System.out.println("\n\n------------1.0----------------");
            Blog blog_parm=new Blog(2,"",null);
            List<Blog> blogList = sqlSession.selectList("zhtt.demo01.Blog.selectBlog2", blog_parm);
            System.out.println(blogList.size()+": "+blogList);
            System.out.println("------------1.1----------------");


            System.out.println("\n\n------------2.0----------------");
            Blog blog_parm2=new Blog(2,"","blo%");
            List<Blog> blogList2 = sqlSession.selectList("zhtt.demo01.Blog.selectBlog2", blog_parm2);
            System.out.println(blogList2.size()+": "+blogList2);
            System.out.println("------------2.1----------------");


        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    @Test
    public void selectBlog_where() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();

            System.out.println("\n\n------------1.0----------------");
            Blog blog_parm=new Blog(2,"",null);
            List<Blog> blogList = sqlSession.selectList("zhtt.demo01.Blog.selectBlog3", blog_parm);
            System.out.println(blogList.size()+": "+blogList);
            System.out.println("------------1.1----------------");


            System.out.println("\n\n------------2.0----------------");
            Blog blog_parm2=new Blog(2,"","blo%");
            List<Blog> blogList2 = sqlSession.selectList("zhtt.demo01.Blog.selectBlog3", blog_parm2);
            System.out.println(blogList2.size()+": "+blogList2);
            System.out.println("------------2.1----------------");


        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    @Test
    public void selectBlog_trim() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();

            System.out.println("\n\n------------1.0----------------");
            Blog blog_parm=new Blog(2,"",null);
            List<Blog> blogList = sqlSession.selectList("zhtt.demo01.Blog.selectBlog4", blog_parm);
            System.out.println(blogList.size()+": "+blogList);
            System.out.println("------------1.1----------------");


            System.out.println("\n\n------------2.0----------------");
            Blog blog_parm2=new Blog(2,"","blo%");
            List<Blog> blogList2 = sqlSession.selectList("zhtt.demo01.Blog.selectBlog4", blog_parm2);
            System.out.println(blogList2.size()+": "+blogList2);
            System.out.println("------------2.1----------------");


        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    @Test
    public void update1(){
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            System.out.println("\n\n------------1.0----------------");
            Blog blog_parm=new Blog(2,"blog2_des","blog2_title");
            blog_parm.setCreateTime(new Date());
            int r=sqlSession.update("zhtt.demo01.Blog.update1",blog_parm);
            System.out.println("------------1.1----------------");
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    @Test
    public void update2(){
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            System.out.println("\n\n------------1.0----------------");
            Blog blog_parm=new Blog(4,"blog4_des","blog4_title");
            blog_parm.setCreateTime(new Date());
            blog_parm.setYear(2017);
            int r=sqlSession.update("zhtt.demo01.Blog.update2",blog_parm);
            System.out.println("------------1.1----------------");
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    @Test
    public void foreach1(){
        List<Integer> idList= Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9});
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            System.out.println("\n\n------------1.0----------------");
            List<Blog> blogList = sqlSession.selectList("zhtt.demo01.Blog.selectIn", idList);
            System.out.println(blogList.size()+": "+blogList);
            System.out.println("------------1.1----------------");
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    @Test
    public void insters(){
        Blog[] blogs={new Blog("desc","title1"),new Blog("desc","title1"),new Blog("desc","title1"),new Blog("desc","title1")};
        List<Blog> blogList= Arrays.asList(blogs);
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            System.out.println("\n\n------------1.0----------------");
            int r = sqlSession.insert("zhtt.demo01.Blog.insters", blogList);
            System.out.println("r："+r);
            System.out.println("------------1.1----------------");
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    /**
     * 存在则更新、不存在在插入
     */
    @Test
    public void merge(){
        SqlSession sqlSession=null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            Blog blog_parm=new Blog(4,"blog4_des2","blog4_title2");/** todo:注意这里只传入了id、des、title三个字段，执行后，其它字段将会转置空 **/
            System.out.println("\n\n------------1.0----------------");
            int r = sqlSession.insert("zhtt.demo01.Blog.replace", blog_parm);
            System.out.println("r："+r);
            System.out.println("------------1.1----------------");
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }
}